package com.iRomantic.controller.manager;

import java.io.File;

import com.iRomantic.common.BaseController;
import com.iRomantic.common.oss.OSSService;
import com.iRomantic.common.util.FileUtil;
import com.iRomantic.config.Constant;
import com.iRomantic.domain.entity.Video;
import com.jfinal.upload.UploadFile;

public class VideoController extends BaseController {
	
	/**
	 * 创建视频
	 * @throws Exception 
	 */
	public void create() throws Exception{
		String path = this.getSession().getServletContext().getRealPath(Constant.TEMP_FOLDER);
		
		//视频
		UploadFile file = this.getFile("video.file",path,200*1024*1024);
		//缩略图
		UploadFile thumbnail = this.getFile("video.thumbnail",path,200*1024*1024);
		Video video = this.getModel(Video.class);
		
		if(file!=null && thumbnail!=null){
			long size = file.getFile().length();
			String fileName = FileUtil.randomFileName(file.getFileName());
			OSSService.getInstance().save(Constant.VIDEO_PICTURE+video.getStr("weddingId")+"/"+fileName, file.getFile());
			file.getFile().delete();
			String thumbnailName = "thumbnail_"+fileName+".jpg";
			OSSService.getInstance().save(Constant.VIDEO_PICTURE+video.getStr("weddingId")+"/"+thumbnailName, thumbnail.getFile());
			thumbnail.getFile().delete();
			video.set("size",size);
			video.set("url", fileName);
			video.set("thumbnailUrl", thumbnailName);
			video.save();
		}
		
		this.renderJson(video);
	}

}
